package com.ybkj.daijia.config;

import com.ybkj.daijia.Setting;
import com.ybkj.daijia.SignHelper;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.HttpServerErrorException;
import org.springframework.web.client.RestTemplate;

@Component
public class PartnerApi {

    /**
     * 多条件查询商家(get)
     */
    public static final String QUERY_PARTNER = "api/partner";
    /**
     * 保存商家基本信息(post)
     */
    public static final String SAVE_PARTNER = "api/partner";
    /**
     * 修改商家信息(put)
     */
    public static final String UPDATE_PARTNER = "api/partner/";
    /**
     * 修改子分类名称
     */
    public static final String UPDATE_INTENT = "api/intent/";
    /**
     * 保存图片
     */
    public static final String SAVE_PARTNERIMG = "api/partnerImg";
    /**
     * 删除商家信息(delete)
     */
    public static final String DELETE_PARTNER = "api/partner/";
    /**
     * 删除分类
     */
    public static final String DELETE_INTENT = "api/intent/";
    /**
     * 查询指定主类 下的分类的集合
     */
    public static final String QUERY_INTENT = "api/intent/findIntents";
    /**
     * 保存子分类
     */
    public static final String SAVE_INTENT = "api/intent";
    /**
     * 根据Id查询一个商家
     */
    public static final String QUERY_ONE = "api/partner/findOne";
    /**
     * 根据id查询一个分类
     */
    public static final String QUERY_ONE_INTENT = "api/intent/findOne";
    private static final Logger logger = LoggerFactory.getLogger(PartnerApi.class);
    @Value("${module.partner.url}")
    private String partnerBaseURL;
    @Value("${module.partner.appId}")
    private String partnerAppid;
    @Value("${module.partner.appKey}")
    private String partnerAppkey;
    @Autowired
    private RestTemplate restTemplate;

    public String getPartnerBaseURL() {

        return partnerBaseURL.endsWith("/") ? partnerBaseURL : partnerBaseURL + "/";
    }

    public void setPartnerBaseURL(String partnerBaseURL) {
        this.partnerBaseURL = partnerBaseURL;
    }

    public String getPartnerAppid() {
        return partnerAppid;
    }

    public void setPartnerAppid(String partnerAppid) {
        this.partnerAppid = partnerAppid;
    }

    public String getPartnerAppkey() {
        return partnerAppkey;
    }

    public void setPartnerAppkey(String partnerAppkey) {
        this.partnerAppkey = partnerAppkey;
    }

    public RestTemplate getRestTemplate() {
        return restTemplate;
    }

    public void setRestTemplate(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    public String get(String apiurl, Map<String, String> kv, Setting setting) {

        kv.put("appKey", setting.getSecKey());

        String sign = SignHelper.sign(SignHelper.buildSignMessage(kv), partnerAppkey);

        kv.put("sign", sign);
        kv.put("appId", partnerAppid);

        String result = null;

        try {
            result = restTemplate.getForObject(apiurl, String.class, kv);
        } catch (HttpClientErrorException e) {
            result = e.getResponseBodyAsString();
        }
        return result;

    }

    public String post(String apiurl, Map<String, String> kv, Setting setting) {

        kv.put("appKey", setting.getSecKey());

        String sign = SignHelper.sign(SignHelper.buildSignMessage(kv), partnerAppkey);

        kv.put("sign", sign);
        kv.put("appId", partnerAppid);

        String result = null;

        try {
            result = restTemplate.postForObject(apiurl, null, String.class, kv);
        } catch (HttpClientErrorException e) {
            logger.error("HttpClientErrorException:code:{},status:{},body:{}", e.getStatusCode(),
                e.getStatusText(), e.getResponseBodyAsString());
            result = e.getResponseBodyAsString();
        } catch (HttpServerErrorException e) {
            logger.error("HttpServerErrorException:{}", e.getResponseBodyAsString());
            result = e.getResponseBodyAsString();
        }

        return result;
    }

    public String put(String apiurl, Map<String, String> kv, Setting setting) {
        kv.put("appKey", setting.getSecKey());

        String sign = SignHelper.sign(SignHelper.buildSignMessage(kv), partnerAppkey);

        kv.put("sign", sign);
        kv.put("appId", partnerAppid);

        String result = null;
        try {
            restTemplate.put(apiurl, null, kv);
        } catch (HttpClientErrorException e) {
            result = e.getResponseBodyAsString();
        }
        return result;

    }

    public String delete(String apiurl, Map<String, String> kv, Setting setting) {

        kv.put("appKey", setting.getSecKey());

        String sign = SignHelper.sign(SignHelper.buildSignMessage(kv), partnerAppkey);

        kv.put("sign", sign);
        kv.put("appId", partnerAppid);
        String result = null;
        try {
            restTemplate.delete(apiurl, kv);
        } catch (HttpClientErrorException e) {
            result = e.getResponseBodyAsString();
        }
        return result;

    }


}
